/**
 * part.js (c) 2021
 * Author: WUYAX
 * Version: V1.0
 * Created: 2021-06-30 10:37
 * Modified:
 * Description: partServer actions
 */
import { getFullNodes } from '@/assets/js/app/utils/sys-utils/tools.js'
/**
 * 筛选出组件树中，实现了dataExchange方法的组件
 * @param {Vue instance} comp vue 实例
 * @returns Array
 */
function findTheComp(comp) {
  if (!Array.isArray(comp)) {
    comp = [comp]
  }
  return comp.reduce((acc, cur) => {
    if (typeof cur.dataExchange === 'function') {
      acc.push(cur)
      return acc
    }
    if (Array.isArray(cur.$children) && cur.$children.length) {
      return acc.concat(findTheComp(cur.$children))
    }
    return acc
  }, [])
}

export default [
  {
    // This applies only to remote data exchange
    id: 'partServer.dataExchange',
    name: '部件数据交换',
    type: 'system',
    exec: (app, param = {}) => {
      const instance = app.partServer.getCompById(param)
      if (!instance) return
      const result = findTheComp(instance)
      if (result.length) {
        const [comp] = result
        const value = comp.dataExchange(param)
        // console.log(value)
        if (value) {
          // send data to remote
          // todo
        }
      }
    }
  },
  // added by WUYAX @2022-01-04
  {
    id: 'partServer.active',
    name: '部件激活',
    type: 'system',
    exec: (app, param = {}) => {
      const { partId, isActive = false } = param
      if (!partId) return
      const instance = app.partServer.getCompById(partId)
      if (instance && instance.$el) {
        const shadow = isActive ? 'inset 0 0 10px 5px red' : ''
        let list = getFullNodes(instance.$el)
        if (list.length) {
          for (let i = 0; i < list.length; i++) {
            list[i].style.boxShadow = shadow
          }
        } else {
          instance.$el.style.boxShadow = shadow
        }
      }
    }
  },
  {
    id: 'partServer.highLight',
    name: '部件高亮',
    type: 'system',
    exec: (app, param = {}) => {
      const { partId, isActive = false } = param
      let parts = document.getElementsByClassName('anov-part')
      const opacity = 1
      const otherOpacity = 0.3
      if (isActive) {
        if (!partId) return
        const instance = app.partServer.getCompById(partId)
        if (instance && instance.$el) {
          for (let i = 0; i < parts.length; i++) {
            parts[i].style.opacity = otherOpacity
            parts[i].style.transition = 'all 0.5s'
          }
          let list = getFullNodes(instance.$el)
          if (list.length) {
            for (let i = 0; i < list.length; i++) {
              list[i].style.opacity = opacity
            }
          } else {
            instance.$el.style.opacity = opacity
          }
        }
      } else {
        for (let i = 0; i < parts.length; i++) {
          parts[i].style.opacity = opacity
          parts[i].style.transition = 'all 0s'
        }
      }
    }
  },
  {
    id: 'auxiliary.toggle',
    name: '显示｜隐藏模块辅助线',
    type: 'system',
    exec: (app, param = {}) => {
      app.partServer.auxiliary = !app.partServer.auxiliary
    }
  }
]
